package com.wifi.view;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.bean.MacBean;
import com.wifi.eshipewifi.ConnetAcitvity;
import com.wifi.eshipewifi.R;
import com.wifi.eshipewifi.SocThread;

import java.util.ArrayList;


public class BlackConnetUserAdapter extends BaseAdapter{

    private Context context;
    BaseApplication application;
    String[] item_data;
    String data;
    String delete_data;
    String myMac;
    ArrayList<MacBean> macBeanArrayList=new ArrayList<MacBean>();
    public BlackConnetUserAdapter(Context context){
        this.context=context;
    }

    public BlackConnetUserAdapter(Context context, String[] data, BaseApplication application, String myMac){
        this.context=context;
//        $scan_useres;
//        20:82:c0:52:d5:0d,RedmiNote2-hongmishouji;
//        0c:30:21:c7:8a:b4,wangfeideiPhone;
//        d8:cb:8a:c5:57:2d,WIN-6VG227O4SL2;----MAC地址
        this.myMac=myMac;
        this.data=data[1];
        this.application=application;
        item_data = data[1].split(",");
        for (int i=0;i<item_data.length;i=i+2){
            MacBean macBean=new MacBean();
            macBean.setMac(item_data[i]);
            macBean.setMacName(item_data[i+1]);
            macBeanArrayList.add(macBean);
        }

    }

    public void setData(){
//        item_data= (delete_data.split(","));
        this.notifyDataSetChanged();//todo
    }

    @Override
    public int getCount(){
        return macBeanArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView = View.inflate(context, R.layout.item_user_connet, null);
            holder=new ViewHolder();
            holder.tv_item_name = (TextView) convertView.findViewById(R.id.tv_item_name);
            holder.tv_item_time = (TextView) convertView.findViewById(R.id.tv_item_time);
            holder.rl_item = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            holder.tv_item_black = (TextView) convertView.findViewById(R.id.tv_item_black);
            holder.cb_conn = (CheckBox) convertView.findViewById(R.id.cb_conn);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_item_black.setVisibility(View.VISIBLE);
        holder.tv_item_black.setText("移除");
//        b0:e2:35:95:3f:82,RedmiNote3,38:bc:1a:eb:2b:73,Meizu;\r\n ----MAC地址
        holder.tv_item_time.setText("MAC地址："+macBeanArrayList.get(position).getMac());
        holder.tv_item_name.setText(macBeanArrayList.get(position).getMacName());//todo
        if(macBeanArrayList.get(position).getMac().indexOf(myMac)!=-1){
            holder.tv_item_name.setText("本机设备");//todo
            holder.tv_item_black.setVisibility(View.GONE);
        }

        holder.tv_item_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    final View view = View.inflate(context, R.layout.item_recover_dialog_text, null);
                    AlertDialog dialog= new AlertDialog.Builder(context)
                            .setTitle("信任")
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setView(view)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    application.sendMsg("$mac_recover;"+macBeanArrayList.get(position).getMac()+","+macBeanArrayList.get(position).getMacName()+";");
                                    Toast.makeText(context,"已成功信任"+macBeanArrayList.get(position).getMacName()+"，请稍等……",Toast.LENGTH_LONG).show();
//                                    delete_data =data.replace(item_data[position] + "," + item_data[position + 1]+",", "");
                                    macBeanArrayList.remove(position);
                                    dialog.cancel();
                                    setData();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            }).create();
                    dialog .show();

            }
        });
        return convertView;
    }


    private class ViewHolder{

        private TextView tv_item_name;
        private TextView tv_item_time;
        private TextView tv_item_black;
        private RelativeLayout rl_item;
        private CheckBox cb_conn;
    }
}
